<?php $title = '帖子'; require './header.php'; ?>

<?php
$info = $_REQUEST['floor'] ? $hlx->getPostFloorInfo($_REQUEST['post_id']) : $hlx->getPostInfo($_REQUEST['post_id']);
$post = $info['post'];
$page = abs((int) $_REQUEST['page'] ?: 1);
$comments = $_REQUEST['floor'] ? $hlx->getPostFloorInfo($_REQUEST['post_id'],$page)['comments'] : $hlx->getPostInfo($_REQUEST['post_id'],$page)['comments'];
$creditsList = $hlx->getPostCreditsList($post['postID']);
?>
<div class="container-fluid">
	<div class="border p-3">
		<h4>
			<?php if($post['scorecount']) { ?>
			<span class="badge badge-danger"><?php echo ($post['scorecount'] > 0 ? '+' : '') . $post['scorecount']; ?></span>
			<?php } ?>
			<?php echo $post['title']; ?>
			<span class="float-right">
				<?php if(time() - $post['createTime'] / 1000 <= 12 * 60 * 60) { ?>
				<span class="badge badge-success">新</span>
				<?php } ?>
				<?php if($post['commentCount'] >= 200) { ?>
				<span class="badge badge-danger">热</span>
				<?php } ?>
				<?php if($post['isGood']) { ?>
				<span class="badge badge-warning text-light">精</span>
				<?php } ?>
				<?php if($post['isAuthention']) { ?>
				<span class="badge badge-info">认</span>
				<?php } ?>
			</span>
		</h4>
		<p class="text-muted">
			<span><?php echo date('Y-m-d H:i:s',$post['createTime'] / 1000); ?></span>
			<span class="float-right">ID：<?php echo $post['postID']; ?></span>
		</p>
		<p class="text-muted">
			<span class="mr-3"><i class="fa fa-eye"></i> <?php echo $post['hit']; ?></span>
			<a class="mr-3 text-muted" href="#comments"><i class="fa fa-commenting-o"></i> <?php echo $post['commentCount']; ?></a>
			<span class="mr-3"><i class="fa fa-thumbs-o-up"></i> <?php echo $post['praise']; ?></span>
			<a class="float-right text-muted" href="./category.php?cat_id=<?php echo $post['category']['categoryID']; ?>">
				<?php echo $post['category']['title']; ?>
				<i class="fa fa-chevron-right"></i>
			</a>
		</p>
		
		<?php if($page == 1) { ?>
		
		<div class="media border border-left-0 border-right-0 pt-3">
			<img src="<?php echo $post['user']['avatar']; ?>" class="mr-3 rounded-circle" style="width:50px;">
			<div class="media-body">
				<h6 color="<?php echo rgbaToRgb($comment['user']['nickColor']); ?>">
					<?php echo $post['user']['nick']; ?>
					
					<span>
						<?php foreach($post['user']['medalList'] as $medal) { ?>
						<img src="<?php echo $medal['url']; ?>" width="18" data-toggle="tooltip" title="<?php echo $medal['name']; ?>">
						<?php } ?>
					</span>
					
					<span class="float-right text-success">楼主</span>
				</h6>
				<p>
					<?php if($post['user']['gender'] == 1) { ?>
					<span class="badge text-white" bgcolor="#FF69B4"><i class="fa fa-mars-stroke"></i> <?php echo $post['user']['age']; ?></span>
					<?php } else { ?>
					<span class="badge text-white" bgcolor="#1E90FF"><i class="fa fa-mars"></i> <?php echo $post['user']['age']; ?></span>
					<?php } ?>
					<?php if($post['user']['identityTitle']) { ?>
					<span class="badge text-white" bgcolor="<?php echo color($post['user']['identityColor']); ?>">
						<?php echo $post['user']['identityTitle']; ?>
					</span>
					<?php } ?>
				</p>
			</div>
		</div>
		<div class="mt-2 detail">
			<?php if($post['isAuthention']) { ?>
			<img class="float-right" style="position:relative;right:100;top:-50;z-index:10" src="./static/images/authention.png" width="200">
			<?php } ?>
			
			<?php
			if($post['recommendTopics']) {
				foreach($post['recommendTopics'] as $recommend) {
					$post['detail'] = str_replace($recommend['title'],"<a href='./post.php?post_id={$recommend['postID']}'>{$recommend['title']}</a>",$post['detail']);
				}
			}
			echo detail($post['detail']);
			?>
			
			<div class="text-center">
				<?php foreach($post['images'] as $img) { ?>
				<img width="100%" src="<?php echo $img; ?>" class="mt-3">
				<?php } ?>
			</div>
		</div>
		
		<?php if($post['scorecount']) { ?>
		<table class="table table-sm mt-3">
			<thead class="thead-light">
				<tr>
					<th width="30%">用户</th>
					<th width="60%">留言</th>
					<th width="10%">数量</th>
				</tr>
			</thead>
			<tbody>
				<?php foreach($creditsList as $credits) { ?>
				<tr>
					<td><img src="<?php echo $credits['avatar']; ?>" width="40" class="rounded"> <?php echo $credits['username']; ?></td>
					<td><?php echo detail($credits['scoreTxt']) ?: '评论已删除'; ?></td>
					<td class="<?php echo $credits['isadmin'] ? 'text-danger' : 'text-success'; ?>">
						<?php if($credits['isadmin']) { ?>
						<img src="./static/images/red_hulu.png" height="20">
						<?php } else { ?>
						<img src="./static/images/normal_hulu.png" height="20">
						<?php } ?>
						<?php echo $credits['score']; ?>
					</td>
				</tr>
				<?php } ?>
			</tbody>
		</table>
		<?php } ?>
		
		<?php if($info['remindUsers']) { ?>
		<div class="border-top p-2 mt-2">
			<i class="fa fa-at text-white p-1 rounded" bgcolor="#3472FC"></i>
			<?php foreach($info['remindUsers'] as $k => $user) { ?>
			<?php echo $k ? '、' : ''; ?>
			<a href="#user"><?php echo $user['nick']; ?></a>
			<?php } ?>
		</div>
		<?php } ?>
		
		<?php } ?>
	</div>
		
	<div id="comments" class="mt-3 p-3 border">
		<h5>
			<span>评论 <small class="text-muted"><?php echo $post['commentCount']; ?></small></span>
			<?php if(! $_REQUEST['floor']) { ?>
			<a class="float-right small" href="<?php echo goParam(['page' => 1,'floor' => 1]); ?>">只看楼主</a>
			<?php } else { ?>
			<a class="float-right small" href="<?php echo goParam(['page' => 1,'floor' => 0]); ?>">查看全部</a>
			<?php } ?>
		</h5>
		
		<ul class="list-group list-group-flush">
			<?php foreach($comments as $k => $comment) { ?>
			<li class="list-group-item">
				<div class="media pt-3">
					<img src="<?php echo $comment['user']['avatar']; ?>" class="mr-3 rounded-circle" style="width:50px;">
					<div class="media-body">
						<h6 color="<?php echo rgbaToRgb($comment['user']['nickColor']); ?>">
							<?php echo $comment['user']['nick']; ?>
							
							<span>
								<?php foreach($comment['user']['medalList'] as $medal) { ?>
								<img src="<?php echo $medal['url']; ?>" width="18" data-toggle="tooltip" title="<?php echo $medal['name']; ?>">
								<?php } ?>
							</span>
							
							<span class="float-right text-success"># <?php echo $comment['seq']; ?></span>
						</h6>
						<p>
							<?php if($comment['user']['gender'] == 1) { ?>
							<span class="badge text-white" bgcolor="#FF69B4"><i class="fa fa-mars-stroke"></i> <?php echo $comment['user']['age']; ?></span>
							<?php } else { ?>
							<span class="badge text-white" bgcolor="#1E90FF"><i class="fa fa-mars"></i> <?php echo $comment['user']['age']; ?></span>
							<?php } ?>
							<?php if($comment['user']['userID'] == $post['user']['userID']) { ?>
							<span class="badge badge-primary">楼主</span>
							<?php } ?>
							<?php if($comment['user']['identityTitle']) { ?>
							<span class="badge text-white" bgcolor="<?php echo color($comment['user']['identityColor']); ?>">
								<?php echo $comment['user']['identityTitle']; ?>
							</span>
							<?php } ?>
							<span class="float-right text-muted"><?php echo date('Y-m-d H:i:s',$comment['createTime'] / 1000); ?></span>
						</p>
						
						<?php if($comment['refComment']) { ?>
						<div class="alert alert-secondary">
							<h6>回复 <?php echo $comment['refComment']['nick']; ?></h6>
							<p ellipsis="3"><?php echo detail($comment['refComment']['text']); ?></p>
						</div>
						<?php } ?>
						
						<p class="detail"><?php echo detail($comment['text']); ?></p>
						
						<?php if($comment['score']) { ?>
						<table class="table table-sm mt-3">
							<thead class="thead-light">
								<tr>
									<th width="30%">用户</th>
									<th width="60%">留言</th>
									<th width="10%">数量</th>
								</tr>
							</thead>
							<tbody>
								<?php $creditsList = $hlx->getCommentCreditsList($comment['commentID']); ?>
								<?php foreach($creditsList as $credits) { ?>
								<tr>
									<td><img src="<?php echo $credits['avatar']; ?>" width="40" class="rounded"> <?php echo $credits['username']; ?></td>
									<td><?php echo detail($credits['scoreTxt']) ?: '评论已删除'; ?></td>
									<td class="<?php echo $credits['isadmin'] ? 'text-danger' : 'text-success'; ?>">
										<?php if($credits['isadmin']) { ?>
										<img src="./static/images/red_hulu.png" height="20">
										<?php } else { ?>
										<img src="./static/images/normal_hulu.png" height="20">
										<?php } ?>
										<?php echo $credits['score']; ?>
									</td>
								</tr>
								<?php } ?>
							</tbody>
						</table>
						<?php } ?>
						
						<ul class="nav float-right comment-action-list">
							<?php if($comment['user']['userID'] != $_User['userID']) { ?>
							<li class="nav-item"><a class="nav-link" href="#" data-toggle="modal" data-target="#reply-<?php echo $comment['commentID']; ?>">回复</a></li>
							<li class="nav-item"><a class="nav-link" href="#">举报</a></li>
							<li class="nav-item"><a class="nav-link" href="#">送葫芦</a></li>
							<?php } ?>
							<?php if($comment['user']['userID'] == $_User['userID'] || $post['user']['userID'] == $_User['userID']) { ?>
							<li class="nav-item"><a class="nav-link" href="#" data-toggle="modal" data-target="#comment-delete-<?php echo $comment['commentID']; ?>">删除</a></li>
							<?php } ?>
							<li class="nav-item"><a class="nav-link" href="#">复制全文</a></li>
						</ul>
						
						<!-- 回复弹窗 -->
						<div class="modal fade" id="reply-<?php echo $comment['commentID']; ?>">
							<div class="modal-dialog modal-lg">
								<div class="modal-content">
									<div class="modal-header">
										<h4 class="modal-title">回复</h4>
										<button type="button" class="close" data-dismiss="modal">&times;</button>
									</div>
									<div class="modal-body">
										<div class="alert alert-secondary">
											<h6><?php echo $comment['user']['nick']; ?></h6>
											<p ellipsis="3"><?php echo detail($comment['text']); ?></p>
										</div>
										<div class="form-group">
											<textarea class="form-control" rows="5" id="reply-detail-<?php echo $comment['commentID']; ?>" placeholder="请输入回复内容，5～2000 字"></textarea>
										</div>
									</div>
									<div class="modal-footer">
										<button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
										<button type="button" class="btn btn-primary reply" data-id="<?php echo $comment['commentID']; ?>">回复</button>
									</div>
								</div>
							</div>
						</div>
						<!-- 回复弹窗 -->
						
						<!-- 删除弹窗 -->
						<div class="modal fade" id="comment-delete-<?php echo $comment['commentID']; ?>">
							<div class="modal-dialog modal-sm">
								<div class="modal-content">
									<div class="modal-header">
										<h5 class="modal-title">你确定要删除此评论吗</h5>
										<button type="button" class="close" data-dismiss="modal">&times;</button>
									</div>
									<div class="modal-body">
										删除后将无法恢复，如果你坚持删除，请点击【确定】，否则请点击【取消】
									</div>
									<div class="modal-footer">
										<button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
										<button type="button" class="btn btn-primary comment-delete" data-id="<?php echo $comment['commentID']; ?>">确定</button>
									</div>
								</div>
							</div>
						</div>
						<!-- 删除弹窗 -->
					</div>
				</div>
			</li>
			<?php } ?>
		</ul>
		<div class="pt-3">
			<ul class="pagination justify-content-center">
				<li class="page-item <?php echo $page == 1 ? 'disabled' : ''; ?>"><a class="page-link" href="<?php echo goParam(['page' => $page - 1]); ?>">上一页</a></li>
				<?php if($info['totalPage'] <= 15) { ?>
					<?php for($i = 1;$i <= $info['totalPage'];$i ++) { ?>
					<li class="page-item <?php echo $i == $page ? 'active' : ''; ?>"><a class="page-link" href="<?php echo goParam(['page' => $i]); ?>"><?php echo $i; ?></a></li>
					<?php } ?>
				<?php } else { ?>
					<?php if($page >= 5) { ?>
					<li class="page-item"><a class="page-link" href="<?php echo goParam(['page' => 1]); ?>">1</a></li>
					<li class="page-item"><a class="page-link" href="javascript:;">…</a></li>
					<li class="page-item"><a class="page-link" href="<?php echo goParam(['page' => $page - 2]); ?>"><?php echo $page - 2; ?></a></li>
					<li class="page-item"><a class="page-link" href="<?php echo goParam(['page' => $page - 1]); ?>"><?php echo $page - 1; ?></a></li>
					<?php } else { ?>
						<?php for($i = 1;$i < $page;$i ++) { ?>
						<li class="page-item"><a class="page-link" href="<?php echo goParam(['page' => $i]); ?>"><?php echo $i; ?></a></li>
						<?php } ?>
					<?php } ?>
					
					<li class="page-item active"><a class="page-link" href="#"><?php echo $page; ?></a></li>
					
					<?php if($page <= $info['totalPage'] - 5) { ?>
					<li class="page-item"><a class="page-link" href="<?php echo goParam(['page' => $page + 1]); ?>"><?php echo $page + 1; ?></a></li>
					<li class="page-item"><a class="page-link" href="<?php echo goParam(['page' => $page + 2]); ?>"><?php echo $page + 2; ?></a></li>
					<li class="page-item"><a class="page-link" href="javascript:;">…</a></li>
					<li class="page-item"><a class="page-link" href="<?php echo goParam(['page' => $info['totalPage']]); ?>"><?php echo $info['totalPage']; ?></a></li>
					<?php } else { ?>
						<?php for($i = $page + 1;$i <= $info['totalPage'];$i ++) { ?>
						<li class="page-item"><a class="page-link" href="<?php echo goParam(['page' => $i]); ?>"><?php echo $i; ?></a></li>
						<?php } ?>
					<?php } ?>
				<?php } ?>
				<li class="page-item <?php echo $page == $info['totalPage'] ? 'disabled' : ''; ?>"><a class="page-link" href="<?php echo goParam(['page' => $page + 1]); ?>">下一页</a></li>
			</ul>
		</div>
		<div class="mt-3">
			<textarea class="form-control" rows="5" id="reply-detail-0" placeholder="请输入回复内容，5～2000 字"></textarea>
			<div class="mt-2">
				<span>
					<button class="btn btn-outline-primary" data-toggle="popover" data-placement="top" data-target="#reply-0-faces" data-content="#reply-0-faces"><i class="fa fa-smile-o" style="font-size:24px"></i></button>
					<button class="btn btn-outline-primary"><i class="fa fa-picture-o" style="font-size:24px"></i></button>
				</span>
				<button class="btn btn-primary reply float-right" data-id="0">回复</button>
			</div>
		</div>
	</div>
</div>

<script>
$(document).ready(function(){
	$('[data-toggle="tooltip"]').tooltip();
	$('[data-toggle="popover"]').popover();
	
	$('.reply').click(function() {
		var id = $(this).data('id');
		var detail = $('#reply-detail-' + id).val();
		if(! detail) $('#reply-detail-' + id).focus();
		else if(detail.getLength() < 5) alert('内容不能少于 5 个字符！');
		else if(detail.getLength() > 2000) alert('内容不能多于 2000 个字符！');
		else {
			$.post('./ajax/createComment.php',{post_id: <?php echo $post['postID']; ?>,comment_id: id,detail: detail},function(data) {
				data = JSON.parse(data);
				alert(data.msg);
				if(data.status) {
					window.location.reload();
				}
			});
		}
	});
	
	$('.comment-delete').click(function() {
		$.post('./ajax/deleteComment.php',{post_id: <?php echo $post['postID']; ?>,comment_id: $(this).data('id')},function(data) {
			data = JSON.parse(data);
			if(data.status) {
				alert('删除成功');
				window.location.reload();
			} else {
				alert(data.msg);
			}
		});
	});
});
</script>
</body>
</html>